package com.akai.controller;

import com.akai.pojo.Article;
import com.akai.pojo.PageBean;
import com.akai.pojo.Result;
import com.akai.service.ArticleService;
import com.akai.utils.JwtUtil;
import com.akai.utils.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController  {

    @Autowired
    private ArticleService articleService;
    @PostMapping
    public Result add(@RequestBody @Validated Article article){
    articleService.add(article);
    return Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ){
        PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }

    @GetMapping("/detail")
    public Result<Article> getArticleDetail(@RequestParam Integer id) {
        if (id == null) {
            return Result.error("ID不能为空");
        }
        Article article = articleService.getArticleDetail(id);
        if (article == null) {
            return Result.error("未找到对应的文章");
        }
        return Result.success(article);
    }

    @PutMapping
    public Result<Article> updateArticle(@RequestBody @Validated Article article){
        articleService.updateArticle(article);
        return Result.success(article);
    }

    @DeleteMapping
    public Result<Article> deleteArticle(@RequestParam Integer id) {
        if (id == null) {
            return Result.error("ID不能为空");
        }

        Article article = articleService.deleteArticle(id);
        if (article == null) {
            return Result.error("未找到对应的文章");
        }
        return Result.success(article);
    }
}
